package com.example.studyapp;

import com.google.gson.Gson;
import org.json.JSONObject;
import java.util.ArrayList;

//解析jstruct结构
public class myjson {
    String ret_null="{\"error\":\"null\"}",ret_true="{\"result\":true}",
            ret_false="{\"result\":false}";
    Gson gson=new Gson();	//解析json
    ArrayList slist=new ArrayList();
    ArrayList clist=new ArrayList();
    public myjson() {
        //为避免每次都要new结构，在这里初始化每个要返回的json结构，
        //使用时直接用Gson转换成json字符串就行
        for(json_enum_dataclass d : json_enum_dataclass.values()) {//逐个初始化结构
            slist.add(new Jstruct(d, json_enum_cs.server));
            clist.add(new Jstruct(d, json_enum_cs.client));
        }
    }
    //获取目标结构
    public Jstruct get(json_enum_dataclass e){
        //返回客户端的数据
        return (Jstruct) clist.get(e.ordinal());
    }
    //根据json字符串解析出jstruct结构
    public json_enum_dataclass dataclass;//tojson识别到的类型
    public Jstruct tojstu(String jsonstr){
        Jstruct  jstu=gson.fromJson(jsonstr,Jstruct.class);
        take_dataclass(jstu.dataclass);
        return jstu;
    }

    //根据dataclass字符串解析
    void take_dataclass(String dclass) {
        dataclass=json_enum_dataclass.dataclass;
        if(dclass==null){
            return;
        }
        if(dclass.isEmpty()) {
            return;
        }
        if(dclass.equals(json_enum_dataclass.login.toString())) {
            this.dataclass=json_enum_dataclass.login;
        }else if(dclass.equals(json_enum_dataclass.logout.toString())) {
            this.dataclass=json_enum_dataclass.logout;
        }else if(dclass.equals(json_enum_dataclass.register.toString())) {
            this.dataclass=json_enum_dataclass.register;
        }else if(dclass.equals(json_enum_dataclass.signup.toString())) {
            this.dataclass=json_enum_dataclass.signup;
        }else if(dclass.equals(json_enum_dataclass.change_username.toString())) {
            this.dataclass=json_enum_dataclass.change_username;
        }else if(dclass.equals(json_enum_dataclass.change_password.toString())) {
            this.dataclass=json_enum_dataclass.change_password;
        }else if(dclass.equals(json_enum_dataclass.paper_list.toString())) {
            this.dataclass=json_enum_dataclass.paper_list;
        }else if(dclass.equals(json_enum_dataclass.paper.toString())) {
            this.dataclass=json_enum_dataclass.paper;
        }else if(dclass.equals(json_enum_dataclass.paper_item.toString())) {
            this.dataclass=json_enum_dataclass.paper_item;
        }else if(dclass.equals(json_enum_dataclass.database_list.toString())) {
            this.dataclass=json_enum_dataclass.database_list;
        }else if(dclass.equals(json_enum_dataclass.database_change.toString())) {
            this.dataclass=json_enum_dataclass.database_change;
        }else if(dclass.equals(json_enum_dataclass.database.toString())) {
            this.dataclass=json_enum_dataclass.database;
        }else if(dclass.equals(json_enum_dataclass.errors_list.toString())) {
            this.dataclass=json_enum_dataclass.errors_list;
        }else if(dclass.equals(json_enum_dataclass.errors.toString())) {
            this.dataclass=json_enum_dataclass.errors;
        }else if(dclass.equals(json_enum_dataclass.book_list.toString())) {
            this.dataclass=json_enum_dataclass.book_list;
        }else if(dclass.equals(json_enum_dataclass.book.toString())) {
            this.dataclass=json_enum_dataclass.book;
        }else if(dclass.equals(json_enum_dataclass.save_book.toString())) {
            this.dataclass=json_enum_dataclass.save_book;
        }else if(dclass.equals(json_enum_dataclass.test.toString())) {
            this.dataclass=json_enum_dataclass.test;
        }
    }
    //将JSONObject转换为字符串
    public String tojson(JSONObject jobj) {
        return gson.toJson(jobj);
    }

}